<template>
  <div class="moduleManage">
    <search-form
      ref="header"
      :search-data="ruleForm"
      :search-form="searchData"
      :search-handle="searchHandle"
    ></search-form>

    <base-table
      :tableData="tableData"
      :tableColumn="tableTitle"
      :tableOption="tableOption"
      :height="tableHeight"
      @action="handleButton"
      :Pagination="page"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    >
      <template v-slot:label="slotData">
        <span>
          <el-tag
            v-for="tag in slotData.data.row.label"
            :key="tag"
            :type="tag"
            style="margin:0 5px 5px;"
          >
            {{ tag }}
          </el-tag>
        </span>
      </template>
    </base-table>

    <!-- 添加或编辑弹框 -->
    <addOrEidtDialog
      ref="moduleDialog"
      v-if="dialogObj.show"
      :Visible="dialogObj.show"
      :type="dialogObj.type"
      :heatSourceId="ruleForm.heatSourceId"
      :dialogRow="dialogObj.dialogRow"
      @close="dialogObj.show = false"
      @refresh="handleSearch"
    ></addOrEidtDialog>
  </div>
</template>
<script>
import searchForm from "@/components/SearchForm/index.vue";
import baseTable from "@/components/Table/index.vue";
import { reactive, toRefs, onMounted } from "vue";
import {
  getRealDataAlone,
  delRealData,
  getLabelData,
} from "@/api/realDataManage";
import { ElMessage, ElMessageBox } from "element-plus";
import addOrEidtDialog from "./component/addOrEditDialog.vue";
import { useStore } from "vuex";
import { getHeatStation } from "@/api/heatStation";
import { getSystemList } from "@/api/system";
import heightMinxin from "@/utils/tableHeight.mixin";
export default {
  name: "realDataManage",
  mixins: [heightMinxin],
  components: { searchForm, baseTable, addOrEidtDialog },
  setup() {
    const store = useStore();
    const state = reactive({
      ruleForm: {
        heatSourceId: "",
        heatStationId: "",
        heatSystemId: "",
        dataName: "",
        label: "",
      },
      searchData: [
        {
          type: "Select",
          prop: "heatSourceId",
          placeholder: "选择热源",
          options: [],
          change: (row) => {
            getHeatStationList(row);
            getSystemData();
          },
        },
        {
          type: "Select",
          prop: "heatStationId",
          placeholder: "选择换热站",
          options: [],
          change: () => {
            getSystemData();
          },
        },
        {
          type: "Select",
          prop: "heatSystemId",
          placeholder: "选择系统",
          options: [],
          change: () => {},
        },
        {
          type: "Select",
          prop: "label",
          options: [],
          placeholder: "选择标签",
          change: () => {},
        },
        {
          type: "Input",
          prop: "dataName",
          placeholder: "数据点名称进行搜索",
        },
      ],
      searchHandle: [
        {
          label: "搜索",
          type: "primary",
          handle: () => {
            handleSearch();
          },
        },
        {
          label: "添加",
          handle: () => {
            if (state.ruleForm.heatSourceId == "") {
              return ElMessage.error("热源标识不能为空");
            }
            handleAdd("add", state.ruleForm);
          },
        },
      ],
      tableData: [],
      tableTitle: [
        { prop: "heatSourceName", label: "热源" },
        { prop: "heatStationName", label: "换热站" },
        { prop: "heatSystemName", label: "系统" },
        { prop: "dataName", label: "数据点名称" },
        { prop: "shortName", label: "简称" },
        { prop: "lowestLimit", label: "底限" },
        { prop: "highLimit", label: "高限" },
        {
          prop: "historyExist",
          label: "是否存历史",
          render(h) {
            if (h.historyExist === 0) {
              return "否";
            } else if (h.historyExist === 1) {
              return "是";
            } else {
              return null;
            }
          },
        },
        {
          prop: "label",
          label: "标签",
          columnType: "slot",
          slotName: "label",
        },
      ],
      tableOption: {
        label: "操作",
        buttons: [
          { name: "编辑", methods: "edit", type: "text" },
          {
            name: "删除",
            methods: "delete",
            style: { color: "red" },
          },
        ],
      },
      page: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      dialogObj: {
        show: false,
        type: "add",
        dialogRow: {},
      },
    });

    onMounted(() => {
      getHeatSourceList(); //获取热源
      getLabelList(); //标签
      handleSearch();
    });

    const getHeatSourceList = async () => {
      let list = store.getters.sourceData;
      state.searchData[0].options = list;
    };

    const getHeatStationList = async (id) => {
      state.ruleForm.heatStationId = "";
      state.searchData[1].options = [];
      if (id == "" || id == null || id == undefined) {
        state.ruleForm.heatStationId = "";
        state.searchData[1].options = [];
        return;
      }

      const { data: res } = await getHeatStation({ heatSourceId: id });
      if (res.success) {
        const list = res.data.map((item) => {
          return { label: item.name, value: item.id };
        });
        state.searchData[1].options = list;
      }
    };

    const getSystemData = async () => {
      state.ruleForm.heatSystemId = "";
      state.searchData[2].options = [];
      const { heatSourceId, heatStationId } = state.ruleForm;
      const { data: res } = await getSystemList({
        heatSourceId,
        heatStationId,
      });
      if (res.success) {
        if (res.data) {
          const list = res.data.map((item) => {
            return { label: item.name, value: item.id };
          });
          state.searchData[2].options = list;
        }
      }
    };

    /**查询标签 */
    const getLabelList = () => {
      getLabelData().then((res) => {
        if (res.data.success) {
          const { data } = res.data;
          const result = data.map((item) => {
            return { label: item.label, value: item.label };
          });
          state.searchData[3].options = result;
        }
      });
    };

    const handleSearch = () => {
      const { pageNo, pageSize } = state.page;
      let params = Object.assign(state.ruleForm, { pageNo, pageSize });
      getRealDataAlone(params).then((res) => {
        if (res.data.success) {
          const { result, total } = res.data.data;
          result.map((item) => {
            item.label = item.label ? eval("(" + item.label + ")") : [];
            return item;
          });
          state.tableData = result;
          state.page.total = total;
        }
      });
    };

    const handleCurrentChange = (val) => {
      state.page.pageNo = val;
      handleSearch();
    };
    const handleSizeChange = (val) => {
      state.page.pageNo = 1;
      state.page.pageSize = val;
      handleSearch();
    };
    const handleAdd = (type, row) => {
      state.dialogObj.show = true;
      state.dialogObj.type = type;
      state.dialogObj.dialogRow = row;
    };
    const handleButton = ({ row, methods, index }) => {
      if (methods == "edit") {
        handleAdd(methods, row);
      } else {
        handleDelete(row, index);
      }
    };

    const handleDelete = (row, index) => {
      ElMessageBox.confirm(`确定删除该实时数据 - ${row.dataName}?`, "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          delRealData({ id: row.id }).then((res) => {
            if (res.data.success) {
              ElMessage.success(res.data.msg);
              state.tableData.splice(index, 1);
            }
          });
        })
        .catch(() => {});
    };

    return {
      ...toRefs(state),
      handleSearch,
      handleAdd,
      handleButton,
      handleCurrentChange,
      handleSizeChange,
    };
  },
};
</script>
